/* 统计分析 */
import React, { PureComponent } from 'react';
import { Table, Row, Col } from 'antd';
import styles from './index.less';
import { history } from 'umi';
import { isLianxi, isCustomeSubject ,isCustome} from '@/utils/utils'
import { statisticsQuestionBank, statisticsQuestionBankOptional } from '@/services/questionsBank'
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            data: [],
            loading: true
        }
    }
    goToQuestionList = (id: any) => () => {
        this.props.changeTabHandle('1', { id })
    }
    componentDidMount() {
        if(isCustome()){
            statisticsQuestionBank().then(res => {
                let data = [];
                for (let i = 0; i < res.length; i++) {
                    const item = res[i];
                    if (!isCustomeSubject(item.id)) {
                        data.push(item)
                    }
                }
                statisticsQuestionBankOptional().then(res => {
                    for (let i = 0; i < res.length; i++) {
                        const item = res[i];
                        if (isCustomeSubject(item.id)) {
                            data.push(item)
                        }
                    }
                    this.setState({
                        data,
                        loading: false
                    })
                })
            })
            return
        }
        statisticsQuestionBank().then(data => {
            this.setState({
                data,
                loading: false
            })
        })
    }

    render() {
        const { data, loading } = this.state;
        const span = !isLianxi() ? 6 : 8
        const columns = [
            {
                title: '科目类别',
                dataIndex: 'title'
            },
            {
                title: <div>
                    <p className={styles.title}>练习题库</p>
                    <Row>
                        <Col span={span}>总数</Col>
                        <Col span={span}>单选</Col>
                        {
                            !isLianxi() && <Col span={span}>多选</Col>
                        }
                        <Col span={span}>判断</Col>
                    </Row>
                </div>,
                dataIndex: 'exam',
                render: (value: any, row: any) => {
                    return <Row>
                        <Col span={span}>{row.lian}</Col>
                        <Col span={span}>{row.lian_dan}</Col>
                        {
                            !isLianxi() && <Col span={span}>{row.lian_duo}</Col>
                        }
                        <Col span={span}>{row.lian_pan}</Col>
                    </Row>
                },
            },
            {
                title: <div>
                    <p className={styles.title}>模拟题库</p>
                    <Row>
                        <Col span={span}>总数</Col>
                        <Col span={span}>单选</Col>
                        {
                            !isLianxi() && <Col span={span}>多选</Col>
                        }
                        <Col span={span}>判断</Col>
                    </Row>
                </div>,
                dataIndex: 'exam',
                render: (value: any, row: any) => {
                    return <Row>
                        <Col span={span}>{row.mo}</Col>
                        <Col span={span}>{row.mo_dan}</Col>
                        {
                            !isLianxi() && <Col span={span}>{row.mo_duo}</Col>
                        }
                        <Col span={span}>{row.mo_pan}</Col>
                    </Row>
                },
            },
            {
                title: <div>
                    <p className={styles.title}>正式题库</p>
                    <Row>
                        <Col span={span}>总数</Col>
                        <Col span={span}>单选</Col>
                        {
                            !isLianxi() && <Col span={span}>多选</Col>
                        }
                        <Col span={span}>判断</Col>
                    </Row>
                </div>,
                dataIndex: 'exam',
                render: (value: any, row: any) => {
                    return <Row>
                        <Col span={span}>{row.zong}</Col>
                        <Col span={span}>{row.zong_dan}</Col>
                        {
                            !isLianxi() && <Col span={span}>{row.zong_duo}</Col>
                        }
                        <Col span={span}>{row.zong_pan}</Col>
                    </Row>
                },
            },
            {
                title: '操作',
                dataIndex: 'id',
                render: (id: any, row: any) => {
                    return <a onClick={this.goToQuestionList(id)}>查看</a>
                }
            }

        ]
        return (
            <div>
                <Table bordered dataSource={data} loading={loading} rowKey="id" columns={columns} />
            </div>
        )
    }
}
